package com.example.springaop.xw;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class MyRunnableTest implements Runnable {

    private Map.Entry<String, Integer> vo;
    private String name;

    public MyRunnableTest(Map.Entry<String, Integer> vo, String name) {
        this.vo = vo;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            System.out.println(name+" begin");
            String remoteFilePath = vo.getKey();
            URL url = new URL(remoteFilePath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5 * 1000);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "image/gif,image/jpeg,image/pjpeg,application/x-shockwaveflash,application/xaml+xml,application/vnd.ms-xpsdocument,application/x-ms-xbap,application/x-ms-application,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*");
            conn.setRequestProperty("Accept-Language", "zh-CN");
            conn.setRequestProperty("Charset", "UTF-8");
            InputStream is = conn.getInputStream();
            byte[] buffer = new byte[1024];
            int hasRead = 0;
            /*while ((hasRead = is.read(buffer))!=-1) {
                System.out.println("" +
                        "我执行了");
            }*/
            System.out.println(name + " end  buffer.length = " + buffer.length);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


//    private String requestPost(String url, Map<String, Object> map, byte[] uploadContent) {
//        String signature = null;
//        try {
//            // 生成signature
//            signature = NRTSignature.gernerateSignature(map, accessKeySecret);
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        CloseableHttpClient client = HttpClients.createDefault();
//        HttpPost httppost = new HttpPost(url);
//        httppost.setHeader("signature", signature);
//        HttpEntity reqEntity = EntityBuilder.create().setBinary(uploadContent)
//                .setContentType(ContentType.create("application/json", CHARSET_UTF8)).build();
//        httppost.setEntity(reqEntity);
//        CloseableHttpResponse response = null;
//        String responseString = null;
//        try {
//            response = client.execute(httppost);
//            int statusCode = response.getStatusLine().getStatusCode();
//            if (statusCode != HttpStatus.SC_OK) {
//                String message = "call servie failed: " + response.getStatusLine();
//                System.out.println(message);
//            }
//            HttpEntity entity = response.getEntity();
//            byte[] responseContent = IOUtils.toByteArray(entity.getContent());
//            responseString = IOUtils.toString(responseContent, CHARSET_UTF8);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            IOUtils.closeQuietly(response);
//        }
//        return responseString;
//    }
//
//
//    public static byte[] getFileStream(String url){
//        try {
//            URL httpUrl = new URL(url);
////            HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection();
////            conn.setRequestMethod("GET");
////            conn.setConnectTimeout(5 * 1000);
////            InputStream inStream = conn.getInputStream();//通过输入流获取数据
//            URLConnection connection=httpUrl.openConnection();
//            connection.setAllowUserInteraction(true);
//            InputStream inStream=connection.getInputStream();
//
//            byte[] bytes = readInputStream(inStream);//得到的二进制数据
//            return bytes;
//        } catch (Exception e) {
//        }
//        return null;
//    }
//
//    public static byte[] readInputStream(InputStream inStream) throws Exception{
//        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];
//        int len = 0;
//        while( (len=inStream.read(buffer)) != -1 ){
//            outStream.write(buffer, 0, len);
//        }
//        inStream.close();
//        return outStream.toByteArray();
//    }
}
